package com.jhd.jiangxi.recycle.adapter;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jhd.jiangxi.R;
import com.jhd.jiangxi.recycle.bean.BaseBean;
import com.jhd.jiangxi.recycle.holder.BaseViewHolder;
import com.jhd.jiangxi.recycle.listener.OnItemClickListener;
import com.jhd.jiangxi.recycle.listener.OnLoadMoreListener;
import com.jhd.jiangxi.recycle.utils.ScreenUtils;

import java.util.List;

public abstract class BaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_FAILED = 2;
    protected Context context;
    protected OnItemClickListener onItemClickListener;
    protected List<? extends BaseBean> list;
    protected boolean isFailed = false;
    protected boolean isHasMore = true;
    private int itemLayout;
    private RecyclerView recyclerView;
    private OnLoadMoreListener onLoadMoreListener;

    public BaseAdapter(Context context, List<? extends BaseBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setItemLayout(int itemLayout) {
        this.itemLayout = itemLayout;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        this.recyclerView.addOnScrollListener(onScrollListener);
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.layout_footer, null);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtils.dip2px(context, 48.0f)));
            return new FooterViewHolder(view);
        } else if (viewType == TYPE_FAILED) {
            View view = LayoutInflater.from(context).inflate(R.layout.layout_failed, null);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtils.dip2px(context, 48.0f)));
            return new FailedViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(itemLayout, null);
            BaseViewHolder holder = new BaseViewHolder(view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.getRootView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null && list.size() > position) {
                    onItemClickListener.onItemClick(list.get(position), position);
                }
            }
        });
        setViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        if (list == null || list.size() == 0) {
            return 0;
        }
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            if (isFailed) {
                return TYPE_FAILED;
            }
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public abstract void setViewHolder(RecyclerView.ViewHolder holder, int position);

    public class FooterViewHolder extends RecyclerView.ViewHolder {
        private TextView tvFooter;
        private ProgressBar progressBar;

        public FooterViewHolder(View itemView) {
            super(itemView);
            tvFooter = itemView.findViewById(R.id.tv_footer);
            progressBar = itemView.findViewById(R.id.progress);
        }

        public TextView getTvFooter() {
            return tvFooter;
        }

        public ProgressBar getProgressBar() {
            return progressBar;
        }
    }

    public class FailedViewHolder extends RecyclerView.ViewHolder {
        private TextView tvFailed;

        public FailedViewHolder(View itemView) {
            super(itemView);
            tvFailed = itemView.findViewById(R.id.tv_failed);
        }

        public TextView getTvFailed() {
            return tvFailed;
        }
    }


    public boolean isFailed() {
        return isFailed;
    }

    public void setFailed(boolean failed) {
        isFailed = failed;
    }

    public boolean isHasMore() {
        return isHasMore;
    }

    public void setHasMore(boolean isHasMore) {
        this.isHasMore = isHasMore;
    }

    private int mLastVisibleItem = 0;
    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE && ((mLastVisibleItem + 1) == getItemCount())) {
                if (onLoadMoreListener != null) onLoadMoreListener.onLoad();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mLastVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
        }
    };
}
